home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr35 / qname31.zip / QNAME3.C < prev    next >
C/C++ Source or Header  |  1993-05-17  |  5KB  |  156 lines

  1. /*  QNAME3.C
  2. =========================================================================
  3.  
  4.     Original program -- QNAME.C
  5.  
  6.     QNAME.C -- renames ATBBS.QWK in current directory to new file name
  7.     that contains current day and month.
  8.     Written by Martin Leon, Ashton Tate Software Support using the
  9.     Microsoft Quick C compiler.
  10.  
  11. =========================================================================
  12.  
  13.     QNAME.C modified to QNAME2.C
  14.     by David J. Semon
  15.  
  16.     March 22, 1991
  17.  
  18.     Program modified to:
  19.  
  20.     1. Compile with Borland Turbo C 2.0 (struct date d) vs Microsoft
  21.       Quick C (dosdate_t today).
  22.     2. Allow any file name to be entered as a command line argument.
  23.     3. Rename the input file to a new file name that contains the first
  24.       three characters of the input file name plus current day, month
  25.       and a sequence letter (A,B,C,D, etc.).
  26.  
  27.       Examples:     Assume DOS date = 03/22/91
  28.                  Assume first file renamed on that date
  29.  
  30.                  ATBBS    becomes  ATB0322A.QWK
  31.                  SEMWARE  becomes  SEM0322A.QWK
  32.                  ABCDEFGH becomes  ABC0322A.QWK
  33.                  AB       becomes  AB0322A.QWK
  34.                  A        becomes  A0322A.QWK
  35.  
  36.     4. If no file name is provided, a message displays correct syntax.
  37.     5. If the file name is not found, a message is displayed saying so.
  38.  
  39. =======================================================================
  40.  
  41.    QNAME2.C  Modified to QNAME3.C  By Henry Gerlach on 5-12-93
  42.  
  43.    1.  remodified to again compile with MS Quick C.
  44.  
  45.    2.  Program now accepts additional arguments; Additional paths
  46.        in which to look for a file matching the prospective new name.
  47.  
  48.    3.  File to be changed need not be in current directory. A path
  49.        may be included with the name.
  50.  
  51.    4.  extension need not be .QWK. if no extension is given, .QWK
  52.        will be assumed.
  53. =======================================================================*/
  54.  
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <io.h>
  58. #include <dos.h>
  59. #include <string.h>
  60. #include <process.h>
  61. FILE *oldfile;
  62. FILE *newfile;
  63. struct dosdate_t today;
  64. char newname[80];
  65. char oldname[80];
  66. char drive[4],dir[40],name[10],ext[5];
  67. char prefix[5],tnewname[80],pnewname[80],junk[20];
  68. int x,exists=0,numpath=2;
  69.  
  70. /*-------------------------------------------------------------------- */
  71. int main(int argc, char *argv[])
  72.   {
  73.   if(argc < 2)                        /* No argument entered */
  74.     {
  75.     printf("Please enter a file name to rename ...\n");
  76.     printf("Syntax: QNAME3 <filename>\n\n");
  77.     exit(1);
  78.     }
  79.   else
  80.     {
  81.     _splitpath(argv[1],drive,dir,name,ext);  /* break name into components */
  82.     if (!strlen(ext)) strcpy (ext,"QWK\0"); /* If no extension was given,
  83.                                              Assume .QWK      */
  84.     if(strlen(name) > 3)         /* If name of file is greater than */
  85.      {                             /*         three chars,            */
  86.       strncat(prefix,name,3);  /* Copy first three characters */
  87.      }
  88.     else                            /*            else             */
  89.      {
  90.       strcpy(prefix,name);     /* Copy entire string */
  91.       }
  92.     _makepath (oldname,drive,dir,name,ext);   /* add path back to name */
  93.     }
  94.  
  95.   /* Make sure file (oldname) exists */
  96.   if((oldfile = fopen(oldname,"rt")) == NULL)
  97.     {
  98.     printf("%s not found...\n\n",oldname);
  99.     return 1;
  100.     }
  101.   else
  102.     /* The file exists and is open. Close it */
  103.    fclose(oldfile);
  104.  
  105.   /* Get today's date */
  106.     _dos_getdate( &today );
  107.  
  108.   /* x = letter of the alphabet.  Start at "A", ASCII 65 */
  109.   x = 65;
  110.  
  111.   /*  Use sprintf() to combine the month and day and the letter into
  112.      a file name.  Check to see if it doesn't exist and rename. Do
  113.      this until letter = "Z" */
  114.   do
  115.     {
  116.     sprintf(tnewname, "%s%02u%02u%c", prefix, today.month, today.day, x);
  117.     _makepath (newname,drive,dir,tnewname,ext);  /* add path back to name  */
  118.     if((newfile = fopen(newname,"rt")) != NULL) exists = 1; /* check current path */
  119.     while ((numpath < argc) && (!exists)){ /* while paths have been entered and name hasn't been found */
  120.         _splitpath(argv[numpath],drive,dir,junk,junk);  /* break argument into parts */
  121.         _makepath (pnewname,drive,dir,tnewname,ext);  /* add path back to name   */
  122.         if((newfile = fopen(pnewname,"rt")) != NULL) exists = 1;  /* check alternate paths */
  123.         numpath++; /* next argument */
  124.     }
  125.     if (!exists)
  126.      {
  127.        /* File name doesn't exist, rename original to unused file name */
  128.       if(rename(oldname,newname) != 0)
  129.        {
  130.        printf("Error renaming %s to %s\n\n",oldname,newname);
  131.        return 1;
  132.        }
  133.      else
  134.         /* If the rename was succesful, we're done with this loop */
  135.       break;
  136.      }
  137.     else
  138.      /* If the file existed, close it and go to next file name */
  139.      fclose(newfile);
  140.      numpath = 2;
  141.      exists = 0;
  142.     }
  143.   while (++x <= 90);
  144.  
  145.   /* If more than 26 files exist for today's date, give error message */
  146.   if(x > 90)
  147.     {
  148.     printf("\nUnable to rename beyond %s%02u%02u%c.QWK\n",today.month, today.day, x - 1 );
  149.     return 1;
  150.     }
  151.  
  152.   /* Rename completed */
  153.   printf("%s renamed to %s\n\n",oldname,newname);
  154.   return 0;
  155.   }
  156.